package com.xiaotao.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xiaotao.dto.GoodsDto;
import com.xiaotao.dto.GroupBuyDto;
import com.xiaotao.entity.GroupBuy;
import com.xiaotao.entity.GroupBuyDetails;
import com.xiaotao.mapper.GroupBuyDetailsMapper;
import com.xiaotao.mapper.GroupBuyMapper;
import com.xiaotao.service.GroupBuyService;
import com.xiaotao.util.ResponseEnum;
import com.xiaotao.util.ResponseUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GroupBuyServiceImpl implements GroupBuyService {
    @Resource
    private GroupBuyMapper groupBuyMapper;
    @Resource
    private GroupBuyDetailsMapper groupBuyDetailsMapper;

    @Override
    public ResponseUtil findAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GroupBuy> list = groupBuyMapper.findAll();
        PageInfo<GroupBuy> groupBuyPageInfo = new PageInfo<>(list);
        return list != null ? ResponseUtil.get(ResponseEnum.OK, groupBuyPageInfo) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Override
    public ResponseUtil findByMerchantId(Integer merchantId) {
        List<GroupBuyDto> list = groupBuyMapper.findByMerchantId(merchantId);
        return list != null ? ResponseUtil.get(ResponseEnum.OK, list) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Transactional
    @Override
    public ResponseUtil save(GroupBuyDto groupBuyDto) {
        //拿到团购信息
        GroupBuy groupBuy = new GroupBuy();
        BeanUtils.copyProperties(groupBuyDto, groupBuy);
        int insert = groupBuyMapper.insert(groupBuy);
        //拿到相关联的商品信息
        List<GoodsDto> goodsList = groupBuyDto.getGoodsList();
        List<GroupBuyDetails> groupBuyDetailsList = new ArrayList<>();
        if (goodsList!=null){
            for (GoodsDto goodsDto : goodsList) {
                GroupBuyDetails groupBuyDetails = new GroupBuyDetails();
                groupBuyDetails.setGroupBuyId(groupBuy.getId());
                groupBuyDetails.setGoodsId(goodsDto.getId());
                groupBuyDetails.setNum(goodsDto.getNum());
                groupBuyDetailsList.add(groupBuyDetails);
            }
        }
        Integer i = groupBuyDetailsMapper.batchInsert(groupBuyDetailsList);
        return insert > 0 && i > 0 ? ResponseUtil.get(ResponseEnum.OK) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Transactional
    @Override
    public ResponseUtil delete(Integer id) {
        //删除团购详情信息
        List<GroupBuyDetails> byGroupId = groupBuyDetailsMapper.findByGroupId(id);
        if (byGroupId!=null&&!byGroupId.isEmpty()){
            groupBuyDetailsMapper.batchDelete(byGroupId);
        }
        //删除团购信息
        int i = groupBuyMapper.deleteByPrimaryKey(id);
        return i > 0 ? ResponseUtil.get(ResponseEnum.OK) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }

    @Transactional
    @Override
    public ResponseUtil update(GroupBuyDto groupBuyDto) {
        //分步更新
        GroupBuy groupBuy = new GroupBuy();
        BeanUtils.copyProperties(groupBuyDto, groupBuy);
        groupBuyMapper.updateByPrimaryKey(groupBuy);
        List<GoodsDto> goodsDtoList = groupBuyDto.getGoodsList();
        //更新团购详情信息
        for (GoodsDto goodsDto : goodsDtoList) {
            GroupBuyDetails groupBuyDetails = new GroupBuyDetails();
            groupBuyDetails.setNum(goodsDto.getNum());
            groupBuyDetails.setGoodsId(goodsDto.getId());
            groupBuyDetails.setGroupBuyId(groupBuyDto.getId());
            groupBuyDetailsMapper.updateByPrimaryKey(groupBuyDetails);
        }
        return ResponseUtil.get(ResponseEnum.OK);
    }

    @Override
    public ResponseUtil findById(Integer id) {
        List<GroupBuyDto> list = groupBuyMapper.findById(id);
        return list != null ? ResponseUtil.get(ResponseEnum.OK, list) :
                ResponseUtil.get(ResponseEnum.FAIL);
    }
}
